#! /usr/bin/env python3
from  XdmaAccess import XdmaAccess
import numpy as np
import time
BRAM_ADDR = 0x0
BRAM_SIZE = 0x10000 #32bitsx16364depth

device = XdmaAccess(0)

# write_dma expects bytearray
data_to_write = np.arange(BRAM_SIZE, dtype=np.uint8)

# print(device.read(0x10000))
# device.write(0x10000, 0x000000002)
# print(device.read(0x10000))

print("Before DMA Write")
dma_before = device.read_dma(BRAM_ADDR,BRAM_SIZE)
print(dma_before)

write_start = time.time()
device.write_dma(BRAM_ADDR, data_to_write)
write_end = time.time()

print("DMA Write took about:" + str(write_end-write_start) + " seconds")
write_speed = int(BRAM_SIZE)/(write_end-write_start)
print("Write Transfer speed:" + str(write_speed/1000000) + "MB/s")

print("After DMA Write")
read_start = time.time()
dma_after = device.read_dma(BRAM_ADDR,BRAM_SIZE)
read_end = time.time()

print("DMA Read took about:" + str(read_end-read_start) + " seconds")
read_speed = int(BRAM_SIZE)/(read_end-read_start)
print("Read Transfer speed:" + str(read_speed/1000000) + "MB/s")


print(np.array_equal(dma_before, dma_after))
print(max(dma_after))
print(data_to_write.dtype)

# By default there are 4 IRQ channels available
#device.wait_irq(0)

